package com.xhq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GirlController {
    @Autowired
    private GirlRespository girlRespository;

    /**
     * 查找所有女生列表
     * @return
     */
    @GetMapping(value = "/girls")
    public List<Girl> girlList(){
        return girlRespository.findAll();
    }

    /**
     * 添加一个女生
     * @param cupsize
     * @param age
     * @return
     */
    @PostMapping (value = "/girls")
    private Girl girlAdd(@RequestParam("capsize") String cupsize,
                           @RequestParam("age") Integer age) {
        Girl girl = new Girl();
        girl.setCapsize(cupsize);
        girl.setAge(age);

        return girlRespository.save(girl);
    }

    //查询一个女生
    @GetMapping(value = "/girls/{id}")
    public Boolean girlFindOne(@PathVariable("id") Integer id){
       return girlRespository.existsById(id);
    }
    //更新
    @PutMapping(value = "/girls/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id,
                           @RequestParam("capsize") String capsize,
                           @RequestParam("age") Integer age){
        Girl g=new Girl();
        g.setAge(age);
        g.setId(id);
        g.setCapsize(capsize);
        return girlRespository.save(g);

    }

    //删除
    @DeleteMapping(value = "/girls/{id}")
   public void girlDelete(@PathVariable("id") Integer id){
        girlRespository.deleteById(id);
    }

    //通过年龄查询
    @GetMapping(value = "/girls/age/{age}")
    public List<Girl> girlListByAge(@PathVariable("age")Integer age){

       return girlRespository.findByAge(age);
    }
}
